﻿var _this = this;
var popup=false;
var watchClose;


$(document).ready(function() {
	loadAddressBookTree();
	
	$('#department_info').hide();
	var lang = getLanguageResource();
//	console.log("ready");
//	console.log(JSON.stringify(lang));
//	console.log(lang["text.client.addressbook.choose_department_msg"]);
	popup.closed=true;
	
	$('#add_department').click(fn_add_department);
	$('#remove_department').click(fn_remove_department);
	$('#modify_department').click(fn_modify_department);
	
});

function setEventPopupClosed(){
	try{
		watchClose = setInterval(function() {
			if (popup.closed) {
				clearTimeout(watchClose);
//				pezAlert("aaaaaaaaaaaa");
				location.reload();
			} 
		}, 400);	
	}catch(e){
		errLog(e);
	}
}



function fn_add_department (){
	log("fn_add_department122222");
	
	try{
		if(popup.closed==undefined||popup.closed){
			var selectedSeq = getSelectedTreeId();
			var url = "popup/add_depart?seq="+selectedSeq;
			popup = openCenterWindow( 400, 200, url);
			setEventPopupClosed();
		}
	}catch(e){
		errLog(e);
	}
	
	
};

function setTreeName(code, name){
	$('#'+code+' > a').html('<ins class="jstree-icon">&nbsp;</ins>'+name);
}

function fn_remove_department(){
	log("fn_remove_department");
	var selectedId = getSelectedTreeId();
	log("fn_remove_department selectedId= "+selectedId);
	var param ={"department_code":selectedId};
	var message = "하위 카테고리도 전부 삭제 되며 복구가 불가능 합니다.\n신중히 생각해주시기 바랍니다.";
	
	pezAlert(message, function(){
		try{
			$.post('action/remove_department.json', param, function(data) {
//				
				pezAlert("정상적으로 변경 되었습니다.", function(){
					location.reload();	
				}, function(){
					location.reload();
				}).show();
				
			}).error(function() { pezAlert("error"); });
		}catch(e){
			errLog(e);
		}
	}, function(){
		return;
	}).type(alertType.CONFIRM)
	.title("삭제 합니까?")
	.lButton("삭제")
	.rButton("취소").show();
	
	
	
};

function fn_modify_department(){
	log("fn_modify_department");

	try{
		var selectedText =  $('#department_name').val();
		if(_this.selectedText == selectedText){
			pezAlert("이름이 변경되지 않았습니다.");
			return;
		}

		var ret =stringValidate(selectedText);
		if(ret){
			pezAlert("한글/영어/숫자 외에는 입력하실 수 없습니다.\n공백 입력 안됌.");
			return;
		}
		var selectedId = getSelectedTreeId();
		var param ={"department_code":selectedId,
				"department_name":selectedText};
		
		$.post('action/modify_department_name.json', param, function(data) {
//			
			setTreeName(data.data.department_code, data.data.department_name);
			_this.selectedText = data.data.department_name;
			pezAlert("정상적으로 변경 되었습니다.");
			
		}).error(function() { pezAlert("error"); });
	}catch(e){
		errLog(e);		
	}
	
};

function loadAddressBookTree(){
	$("#depart_tree").bind("loaded.jstree",
			function(e, data) {
				data.inst.open_all(-1);
			}).jstree({
		"plugins" : [ "themes", "html_data", "ui" ]
	}).delegate("a", "click", function(event, data) {
		event.preventDefault();	// a tag 무시
	}).bind("select_node.jstree", function(event, data) {
		var department_code = data.rslt.obj.attr("id");
		department_getMemberInGroup(department_code);
	});
}
function department_getMemberInGroup(department_code){
	
	$('#department_info').show();
	var selectedId = getSelectedTreeId();
	$('#department_code').text(selectedId);
	var selectedText =  $('#'+selectedId+' > a').text().trim();
	$('#department_name').val(selectedText);
	_this.selectedText = selectedText;	// 비교를 위한 저장.
}

function getSelectedTree(){
	return $('#depart_tree').find('.jstree-clicked');
}

function getSelectedTreeId(){
	return getSelectedTree().parent().attr('id');
}

function errLog(e){
//	console.log(e);
}

function log(e){
	errLog("======== Log Start ========");
	errLog(e);
	errLog("======== Log the end ========");
}